package com.gitee.wsl.compose.ui.theme

import androidx.compose.foundation.text.selection.LocalTextSelectionColors
import androidx.compose.foundation.text.selection.TextSelectionColors
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.Typography
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import com.gitee.wsl.common.ui.theme.Button
import com.gitee.wsl.common.ui.theme.ColorPaletteScheme
import com.gitee.wsl.common.ui.theme.ColorPalette_LIGHT
import com.gitee.wsl.common.ui.theme.ComposeColorPaletteScheme
import com.gitee.wsl.common.ui.theme.Style
import com.gitee.wsl.common.ui.theme.Theme
import com.gitee.wsl.common.ui.theme.TypographyScheme
import com.gitee.wsl.common.ui.theme.defaultStyle
import com.gitee.wsl.common.ui.theme.merge
import com.gitee.wsl.compose.ui.theme.ThemeExt.color
import com.gitee.wsl.struct.dict.sampleCacheDictionary


object ThemeExt {

  val colorCache= sampleCacheDictionary<Long,Color> { Color(it) }

  fun color(value:Long):Color = colorCache.get(value)!!

}

fun Long.toColor() = color(this)

const val defaultColorSchemeName = ColorPalette_LIGHT

val defaultTheme by lazy{
  Theme.defaultTheme(defaultColorSchemeName)
}

val LocalTheme = staticCompositionLocalOf { defaultTheme }

val currentTheme: Theme<ColorPaletteScheme, TypographyScheme>
  @Composable
  get() {
    return LocalTheme.current
  }

val currentThemeColor: ComposeColorPaletteScheme<Long>
  @Composable
  get() {
    return LocalTheme.current.colorSchemeCompose
  }

val currentThemeTypography: TypographyScheme
  @Composable
  get() {
    return  LocalTheme.current.typography
  }

fun test(){
  val buttonStyle = Style.Button.defaultStyle(defaultTheme)
  buttonStyle.textStyle.merge {

  }
}

/**
 * Uses for override theme configuration
 */
@Composable
fun ThemeConfiguration(
  colors: ColorPaletteScheme = currentTheme.colorScheme,
  typography: TypographyScheme = currentTheme.typography,
  shapes: Shapes = MaterialTheme.shapes,
  compactMode: Boolean = LocalCompactMode.current,
  content: @Composable () -> Unit
) {
  LocalTheme.current.config.colorScheme = colors
  LocalTheme.current.config.typography = typography
  MaterialTheme(
    colorScheme = colors.toCompose().toColorScheme(),
    shapes = shapes,
    typography = typography.toComposeTheme()
  ){
    CompositionLocalProvider(
      //LocalAcrylicPopupEnabled provides useAcrylicPopup,
      LocalTextSelectionColors provides TextSelectionColors(
        colors.text.toColor(),
        colors.background.toColor().copy(0.4f)
      ),
      LocalCompactMode provides compactMode,
      //LocalContentDialog provides contentDialogHostState,
      content = content
    )
  }
}

fun ComposeColorPaletteScheme<Long>.toColorScheme()
= ColorScheme(
  primary = primary.toColor(),
  onPrimary = onPrimary.toColor(),
  primaryContainer = primaryContainer.toColor(),
  onPrimaryContainer = onPrimaryContainer.toColor(),
  inversePrimary = inversePrimary.toColor(),
  secondary = secondary.toColor(),
  onSecondary = onSecondary.toColor(),
  secondaryContainer = secondaryContainer.toColor(),
  onSecondaryContainer = onSecondaryContainer.toColor(),
  tertiary = tertiary.toColor(),
  onTertiary = onTertiary.toColor(),
  tertiaryContainer = tertiaryContainer.toColor(),
  onTertiaryContainer = onTertiaryContainer.toColor(),
  background = background.toColor(),
  onBackground = onBackground.toColor(),
  surface = surface.toColor(),
  onSurface = onSurface.toColor(),
  surfaceVariant = surfaceVariant.toColor(),
  onSurfaceVariant = onSurfaceVariant.toColor(),
  surfaceTint = surfaceTint.toColor(),
  inverseSurface = inverseSurface.toColor(),
  inverseOnSurface = inverseOnSurface.toColor(),
  error = error.toColor(),
  onError = onError.toColor(),
  errorContainer = errorContainer.toColor(),
  onErrorContainer = onErrorContainer.toColor(),
  outline = outline.toColor(),
  outlineVariant = outlineVariant.toColor(),
  scrim = scrim.toColor(),
  surfaceBright = Color.Unspecified,
  surfaceDim = Color.Unspecified,
  surfaceContainer = Color.Unspecified,
  surfaceContainerHigh = Color.Unspecified,
  surfaceContainerHighest = Color.Unspecified,
  surfaceContainerLow = Color.Unspecified,
  surfaceContainerLowest = Color.Unspecified,
)

fun TypographyScheme.toComposeTheme()
=Typography(
  displayLarge = displayLarge,
  displayMedium = displayMedium,
  displaySmall = displaySmall,
  headlineLarge = headlineLarge,
  headlineMedium = headlineMedium,
  headlineSmall = headlineSmall,
  titleLarge = titleLarge,
  titleMedium = titleMedium,
  titleSmall = titleSmall,
  bodyLarge = bodyLarge,
  bodyMedium = bodyMedium,
  bodySmall = bodySmall,
  labelLarge = labelLarge,
  labelMedium = labelMedium,
  labelSmall = labelSmall
)



